﻿// 类
var Class = {
    create : function(){
        return function(){
            this.initialize.apply(this,arguments);
        }
    }
}

// Ajax 对象
var Ajax = Class.create();

Ajax.prototype = {
    _httpRequest : null,          // httpRequest
    resultFuncr  : null,          // 返回等待方法
    resultFuncb  : null,          // 返回方法
    isPost       : null,          // 请求方式post或get
    isAsy        : null,          // 异步
    url          : null,          // 地址
    parameter    : null,          // 参数
    
    initialize : function(url,parameter,isPost,isAsy,funread,funback){
        this.url = url;
        this.parameter = parameter;
        this.isPost = isPost;
        this.isAsy = isAsy;
        this.resultFuncr = funread;
        this.resultFuncb = funback;
    },
    
    send : function(){
        if(window.ActiveXObject){
            // IE浏览器
            try { this._httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
            catch(ex) { this._httpRequest = new ActiveXObject("Microsoft.XMLHTTP");}
        }else if(window.XMLHttpRequest){
            // 非IE浏览器
            try { this._httpRequest = new XMLHttpRequest(); } 
            catch(ex) { this._httpRequest = window.createRequest(); }
            // Set request mime is text/xml
            if(this._httpRequest.overrideMimeType)
                this._httpRequest.overrideMimeType('text/xml');
        }
        
        if(this._httpRequest != null){
            
            if(this.isPost)
                this._httpRequest.open("POST",this.url,this.isAsy);
            else
                this._httpRequest.open("GET",this.url + (this.parameter == null || this.parameter == "" ? "" : "?" + this.parameter),this.isAsy);
            
            var request = this._httpRequest;
            var funcr = this.resultFuncr;
            var funcb = this.resultFuncb;
            
            // 异步回调
            if(this.isAsy)
                this._httpRequest.onreadystatechange = function(){ Callback(request, funcr, funcb); };

            if(this.isPost){
                this._httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                this._httpRequest.setRequestHeader("Content-length", this.parameter.length);
                this._httpRequest.setRequestHeader("Connection", "close");
            }
                
            // 清除缓存
            this._httpRequest.setRequestHeader("If-Modified-Since","0");
            this._httpRequest.setRequestHeader("Cache-Control","no-cache");     

            // 发送请求
            if(this.isPost)
                this._httpRequest.send(this.parameter); 
            else
                this._httpRequest.send(this.parameter); 
                
            // 同步回调    
            if(!this.isAsy)
                Callback(request, funcr, funcb);
        }
    }
}

// 静态方法
Callback = function(request, funcr, funcb){
  
    if(request.readyState == 4){
        // 请求状态为4表示成功
        if(request.status == 200){
            // http状态200表示成功
            if(funcb != null)
                eval(funcb + "('" + escape(request.responseText) + "');");
        }
    }else{
        if(request.readyState == 1){
            if(funcr != null)
                eval(funcr + "();");
        }        
    }
}